﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using BMSQ.DBUtility;//Please add references
namespace BMSQ.DAL
{
	/// <summary>
	/// 数据访问类:UserList
	/// </summary>
	public partial class UserList
	{
		public UserList()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string User_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from UserList");
			strSql.Append(" where User_ID=@User_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@User_ID", SqlDbType.NVarChar,50)			};
			parameters[0].Value = User_ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(BMSQ.Model.UserList model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into UserList(");
			strSql.Append("User_ID,User_Name,User_Phone,User_Email,User_Pwd,Check_Code,User_TrueName,User_Sex,User_Address,User_Image,User_Money,Created_Date,Login_Date)");
			strSql.Append(" values (");
			strSql.Append("@User_ID,@User_Name,@User_Phone,@User_Email,@User_Pwd,@Check_Code,@User_TrueName,@User_Sex,@User_Address,@User_Image,@User_Money,@Created_Date,@Login_Date)");
			SqlParameter[] parameters = {
					new SqlParameter("@User_ID", SqlDbType.NVarChar,50),
					new SqlParameter("@User_Name", SqlDbType.NVarChar,50),
					new SqlParameter("@User_Phone", SqlDbType.NVarChar,20),
					new SqlParameter("@User_Email", SqlDbType.NVarChar,50),
					new SqlParameter("@User_Pwd", SqlDbType.NVarChar,50),
					new SqlParameter("@Check_Code", SqlDbType.NVarChar,10),
					new SqlParameter("@User_TrueName", SqlDbType.NVarChar,50),
					new SqlParameter("@User_Sex", SqlDbType.Int,4),
					new SqlParameter("@User_Address", SqlDbType.NVarChar,200),
					new SqlParameter("@User_Image", SqlDbType.NVarChar,100),
					new SqlParameter("@User_Money", SqlDbType.Int,4),
					new SqlParameter("@Created_Date", SqlDbType.DateTime),
					new SqlParameter("@Login_Date", SqlDbType.DateTime)};
			parameters[0].Value = model.User_ID;
			parameters[1].Value = model.User_Name;
			parameters[2].Value = model.User_Phone;
			parameters[3].Value = model.User_Email;
			parameters[4].Value = model.User_Pwd;
			parameters[5].Value = model.Check_Code;
			parameters[6].Value = model.User_TrueName;
			parameters[7].Value = model.User_Sex;
			parameters[8].Value = model.User_Address;
			parameters[9].Value = model.User_Image;
			parameters[10].Value = model.User_Money;
			parameters[11].Value = model.Created_Date;
			parameters[12].Value = model.Login_Date;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(BMSQ.Model.UserList model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update UserList set ");
			strSql.Append("User_Name=@User_Name,");
			strSql.Append("User_Phone=@User_Phone,");
			strSql.Append("User_Email=@User_Email,");
			strSql.Append("User_Pwd=@User_Pwd,");
			strSql.Append("Check_Code=@Check_Code,");
			strSql.Append("User_TrueName=@User_TrueName,");
			strSql.Append("User_Sex=@User_Sex,");
			strSql.Append("User_Address=@User_Address,");
			strSql.Append("User_Image=@User_Image,");
			strSql.Append("User_Money=@User_Money,");
			strSql.Append("Created_Date=@Created_Date,");
			strSql.Append("Login_Date=@Login_Date");
			strSql.Append(" where User_ID=@User_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@User_Name", SqlDbType.NVarChar,50),
					new SqlParameter("@User_Phone", SqlDbType.NVarChar,20),
					new SqlParameter("@User_Email", SqlDbType.NVarChar,50),
					new SqlParameter("@User_Pwd", SqlDbType.NVarChar,50),
					new SqlParameter("@Check_Code", SqlDbType.NVarChar,10),
					new SqlParameter("@User_TrueName", SqlDbType.NVarChar,50),
					new SqlParameter("@User_Sex", SqlDbType.Int,4),
					new SqlParameter("@User_Address", SqlDbType.NVarChar,200),
					new SqlParameter("@User_Image", SqlDbType.NVarChar,100),
					new SqlParameter("@User_Money", SqlDbType.Int,4),
					new SqlParameter("@Created_Date", SqlDbType.DateTime),
					new SqlParameter("@Login_Date", SqlDbType.DateTime),
					new SqlParameter("@User_ID", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.User_Name;
			parameters[1].Value = model.User_Phone;
			parameters[2].Value = model.User_Email;
			parameters[3].Value = model.User_Pwd;
			parameters[4].Value = model.Check_Code;
			parameters[5].Value = model.User_TrueName;
			parameters[6].Value = model.User_Sex;
			parameters[7].Value = model.User_Address;
			parameters[8].Value = model.User_Image;
			parameters[9].Value = model.User_Money;
			parameters[10].Value = model.Created_Date;
			parameters[11].Value = model.Login_Date;
			parameters[12].Value = model.User_ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string User_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from UserList ");
			strSql.Append(" where User_ID=@User_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@User_ID", SqlDbType.NVarChar,50)			};
			parameters[0].Value = User_ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string User_IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from UserList ");
			strSql.Append(" where User_ID in ("+User_IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BMSQ.Model.UserList GetModel(string User_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 User_ID,User_Name,User_Phone,User_Email,User_Pwd,Check_Code,User_TrueName,User_Sex,User_Address,User_Image,User_Money,Created_Date,Login_Date from UserList ");
			strSql.Append(" where User_ID=@User_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@User_ID", SqlDbType.NVarChar,50)			};
			parameters[0].Value = User_ID;

			BMSQ.Model.UserList model=new BMSQ.Model.UserList();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BMSQ.Model.UserList DataRowToModel(DataRow row)
		{
			BMSQ.Model.UserList model=new BMSQ.Model.UserList();
			if (row != null)
			{
				if(row["User_ID"]!=null)
				{
					model.User_ID=row["User_ID"].ToString();
				}
				if(row["User_Name"]!=null)
				{
					model.User_Name=row["User_Name"].ToString();
				}
				if(row["User_Phone"]!=null)
				{
					model.User_Phone=row["User_Phone"].ToString();
				}
				if(row["User_Email"]!=null)
				{
					model.User_Email=row["User_Email"].ToString();
				}
				if(row["User_Pwd"]!=null)
				{
					model.User_Pwd=row["User_Pwd"].ToString();
				}
				if(row["Check_Code"]!=null)
				{
					model.Check_Code=row["Check_Code"].ToString();
				}
				if(row["User_TrueName"]!=null)
				{
					model.User_TrueName=row["User_TrueName"].ToString();
				}
				if(row["User_Sex"]!=null && row["User_Sex"].ToString()!="")
				{
					model.User_Sex=int.Parse(row["User_Sex"].ToString());
				}
				if(row["User_Address"]!=null)
				{
					model.User_Address=row["User_Address"].ToString();
				}
				if(row["User_Image"]!=null)
				{
					model.User_Image=row["User_Image"].ToString();
				}
				if(row["User_Money"]!=null && row["User_Money"].ToString()!="")
				{
					model.User_Money=int.Parse(row["User_Money"].ToString());
				}
				if(row["Created_Date"]!=null && row["Created_Date"].ToString()!="")
				{
					model.Created_Date=DateTime.Parse(row["Created_Date"].ToString());
				}
				if(row["Login_Date"]!=null && row["Login_Date"].ToString()!="")
				{
					model.Login_Date=DateTime.Parse(row["Login_Date"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select User_ID,User_Name,User_Phone,User_Email,User_Pwd,Check_Code,User_TrueName,User_Sex,User_Address,User_Image,User_Money,Created_Date,Login_Date ");
			strSql.Append(" FROM UserList ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" User_ID,User_Name,User_Phone,User_Email,User_Pwd,Check_Code,User_TrueName,User_Sex,User_Address,User_Image,User_Money,Created_Date,Login_Date ");
			strSql.Append(" FROM UserList ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM UserList ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.User_ID desc");
			}
			strSql.Append(")AS Row, T.*  from UserList T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "UserList";
			parameters[1].Value = "User_ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
        public bool CreateNewUserCheckCode(string phone, string checkCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from UserList");
            strSql.Append(" where Phone=@Phone ");
            SqlParameter[] parameters = {
					new SqlParameter("@Phone", SqlDbType.NVarChar,50)			};
            parameters[0].Value = phone;

            if (DbHelperSQL.Exists(strSql.ToString(), parameters))
            {
                return false;
            }
            else
            {
                Model.UserList model = new Model.UserList();
                model.User_ID = Guid.NewGuid().ToString();
                model.User_Phone = phone;
                model.Check_Code = checkCode;
               return Add(model);
            }
        }

        public bool CreateUserChangePhoneCheckCode(string userID, string phone, string checkCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from UserList");
            strSql.Append(" where Phone=@Phone ");
            SqlParameter[] parameters = {
					new SqlParameter("@Phone", SqlDbType.NVarChar,50)			};
            parameters[0].Value = phone;

            if (DbHelperSQL.Exists(strSql.ToString(), parameters))
            {
                return false;
            }
            else
            {
                Model.UserList model = GetModel(userID);
                model.Check_Code = checkCode;
                return Update(model);
            }
        }

        public bool CreateUserForgetPwdCheckCode(string userID, string checkCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update UserList");
            strSql.Append("set Check_Code=@checkcode ");
            strSql.Append(" where user_id=@user_id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@checkcode", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50)			};
            parameters[0].Value = checkCode;
            parameters[1].Value =userID;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters)>0;
        }
		#endregion  ExtensionMethod
	}
}

